<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
	<head>
		<th:block th:include="include :: header('选择方法')" />
		<link th:href="@{/css/common.css}" rel="stylesheet"/>
		<link th:href="@{/css/choose_data_map.css}" rel="stylesheet"/>
	</head>
	<body>
		<div class="wrapper wrapper-content field">
			<form id="form-virtualUsage">
				<div class="content">
					<div class="search-bg">
						<div class="search-img" id="searchDiv"></div>
						<input type="text" class="search" id="searchValue">
					</div>
					<input type="hidden" name="relationInterfaceId" id="id" th:value="${id}">
					<input type="hidden" name="type" id="type" value="1">
					<input type="hidden" name="appType" id="appType" th:value="${appType}">
					<table class="table table-hover" style = "margin-top:10px;">
						<tbody></tbody>
					</table>
				</div>
			</form>
		</div>
	</body>
	<th:block th:include="include :: footer"/>
	<script type="text/javascript">
		var prefix = ctx + "interface/interface";

		$(function () {
			$("#searchDiv").click(function() {
				loadInterface();
			});

			loadInterface();
		})

		function loadInterface() {
			var config = {
				url: prefix + "/methodList",
				type: "post",
				dataType: "json",
				data: "fbType=1&searchValue="+$("#searchValue").val() + "&id="+$("#id").val()+ "&appType="+$("#appType").val(),
				beforeSend: function () {
					$.modal.loading("正在处理中，请稍后...");
				},
				success: function(result) {
					$("table tbody").empty();
					$.modal.closeLoading();
					if (result.code == web_status.SUCCESS) {
						if (result.data && result.data.length > 0) {
							var data = {};
							data.list = result.data || [];
							var paramsTplHtml = $("#paramsTpl").html();
							laytpl(paramsTplHtml).render(data, function (html) {
								$("table tbody").append(html);
							});
						}
					} else if (result.code == web_status.WARNING) {
						$.modal.alertWarning(result.msg)
					} else {
						$.modal.alertError(result.msg);
					}
				}
			};
			$.ajax(config);
		}

		function checkboxClick() {
			var count = 0;
			$('table').find(':checkbox').each(function(){
				if ($(this).is(":checked")) {
					count ++;
				}
			});
			$("#virtualUsageSelected",window.parent.document).text(count);
		}

		function submitHandler() {
			var count = $("#virtualUsageSelected",window.parent.document).text();
			if (count > 0) {
				var config = {
					url: prefix + "/saveVirtualUsageNew",
					type: "post",
					async: false,
					dataType: "json",
					data: $('#form-virtualUsage').serialize(),
					beforeSend: function () {
						$.modal.loading("正在处理中，请稍后...");
						$.modal.disable();
					},
					success: function(result) {
						if (result.code == web_status.SUCCESS) {
							$.modal.close();
						} else if (result.code == web_status.WARNING) {
							$.modal.alertWarning(result.msg)
						}  else {
							$.modal.alertError(result.msg);
						}
						$.modal.closeLoading();
					}
				};
				$.ajax(config)
			} else {
				$.modal.alertWarning("请选择接口");
			}
		}
	</script>
	<script id="paramsTpl" type="text/template">
		{{# layui.each(d.list, function(index, a){ }}
		<tr>
			<td><input name="interfaceId" onclick="checkboxClick()" type="checkbox" value="{{a.id}}"></td>
			<td>{{a.interfaceName}}</td>
			<td>{{a.interfaceUrl}}</td>
		</tr>
		{{# }); }}
	</script>
</html>
